package com.polaris.live.adapter.luckybag

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.databinding.AdapterLuckyBagHistoryReceiveBinding
import com.polaris.live.databinding.AdapterLuckyBagHistorySendBinding
import com.polaris.live.resp.back_resp.luckybag.LuckyBagReceiveResp
import com.polaris.live.resp.back_resp.luckybag.LuckyBagSendResp
import com.polaris.live.translation.R.string
import com.polaris.live.ui.luckybag.fragment.LuckyBagHistoryFragment
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * LuckyBagHistoryAdapter
 *
 * @author Created by lucas on 2024/6/11 09:56
 * @since 1.0.0
 */
class LuckyBagHistoryAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    override fun getDefItemViewType(position: Int): Int {
        val item = data[position]
        return if (item is LuckyBagSendResp) {
            LuckyBagHistoryFragment.SEND
        } else {
            LuckyBagHistoryFragment.RECEIVED
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseViewHolder {
        return when (viewType) {
            LuckyBagHistoryFragment.SEND -> LuckyBagHistorySendViewHolder(
                AdapterLuckyBagHistorySendBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            )

            LuckyBagHistoryFragment.RECEIVED -> LuckyBagHistoryReceiveViewHolder(
                AdapterLuckyBagHistoryReceiveBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (item) {
            is LuckyBagSendResp -> {
                if (holder is LuckyBagHistorySendViewHolder) {
                    holder.binding.apply {
                        tvTime.text = TimeUtils.millis2String(item.createTime, "MM/dd HH:mm")
                        tvBomb.text = "${item.bombCount}/${item.amount}"
                        tvReceive.text = "${item.receiveCount}/${item.amount}"
                        tvNum.text = item.coins.toString()
                        tvState.visibility = View.GONE
                        when (item.status) {
                            CommonConst.LuckyBagStatus.EXPIRED -> {
                                tvState.visibility = View.VISIBLE
                                tvState.text = StringUtils.getString(string.expired)
                            }

                            CommonConst.LuckyBagStatus.COMPLETED -> {
                                tvState.visibility = View.VISIBLE
                                tvState.text = StringUtils.getString(string.completed)
                            }
                        }
                    }
                }
            }

            is LuckyBagReceiveResp -> {
                if (holder is LuckyBagHistoryReceiveViewHolder) {
                    holder.binding.apply {
                        avAvatar.setImageUrl(item.ownerUserBasic.avatar, item.ownerUserBasic.userId)
                        avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(item.ownerUserBasic.avatarFrame))
                        tvNickname.text = "${StringUtils.getString(string.from)} ${item.ownerUserBasic.nickname}"
                        tvCoin.text = item.coins.toString()
                        tvTime.text = TimeUtils.millis2String(item.createTime, "MM/dd HH:mm")
                        if (item.bombStatus.toBoolean()) {
                            ivBackColor.setImageResource(R.drawable.lucky_history_bomb_back)
                            ivItemTypeIcon.setImageResource(R.drawable.lucky_history_bomb_icon)
                        } else {
                            if (item.luckyStatus.toBoolean()) {
                                ivBackColor.setImageResource(R.drawable.lucky_history_bag_back)
                                ivItemTypeIcon.setImageResource(R.drawable.lucky_history_bag_icon)
                            } else {
                                ivBackColor.setImageResource(0)
                                ivItemTypeIcon.setImageResource(0)
                            }
                        }
                    }
                }
            }
        }
    }

    class LuckyBagHistorySendViewHolder(val binding: AdapterLuckyBagHistorySendBinding) :
        BaseViewHolder(binding.root)

    class LuckyBagHistoryReceiveViewHolder(val binding: AdapterLuckyBagHistoryReceiveBinding) :
        BaseViewHolder(binding.root)
}
